/*
 * StartApplication.java
 *
 * Created on May 22, 2008 12:45:10 AM;
 */

package com.iaroc.irobot.sample;

import com.iaroc.irobot.IRobotCreate;
import com.iaroc.irobot.IRobotCreateConstants;
import com.iaroc.irobot.util.XLights;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.sensorboard.peripheral.ILightSensor;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.spot.util.*;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet
    {
    public static final int SW_1 = 0;
    public static final int SW_2 = 1;
    
    protected void startApp() throws MIDletStateChangeException 
        {                   
        EDemoBoard demo = EDemoBoard.getInstance();
        boolean open1 = true, open2= true;
        System.out.println("Choose a switch.");
        while(open1 && open2)
            {
            if(demo.getSwitches()[0].isClosed())
                {
                try
                    {
                    TestEventHandler teh = new TestEventHandler();
                    teh.getBoard().getLEDs()[0].setColor(LEDColor.TURQUOISE);
                    teh.getBoard().getLEDs()[0].setOn(true);
                    open1 = false;
                    while(true)
                        teh.pollSensors();
                    }
                catch(Exception ex)
                    {
                    System.err.println(ex.getMessage());
                    System.err.println(ex);
                    }
                }
            if(demo.getSwitches()[1].isClosed())
                {   
                try
                    {
                    demo.getLEDs()[7].setColor(LEDColor.BLUE);
                    demo.getLEDs()[7].setOn(true);
                    FinalAttemptEventHandler finalFollower = 
                            new FinalAttemptEventHandler();
                    open2 = false;
                    while(true)
                        finalFollower.pollSensors();
                    }
                catch(Exception ex)
                    {
                    System.err.println(ex.getMessage());
                    System.err.println(ex);
                    }
                }
            }        
        }

    protected void pauseApp() 
        {
        // This is not currently called by the Squawk VM
        return;
        }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) 
            throws MIDletStateChangeException 
        {
        return;
        }
    }
